package com.fruit.system.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.fruit.auth.model.Privilege;
import com.fruit.auth.model.User;
import com.fruit.auth.model.Privilege.PrivilegeTypeEnum;
import com.fruit.auth.service.PrivilegeService;
import com.fruit.dao.BaseDao;
import com.fruit.service.impl.BaseServiceImpl;
import com.fruit.system.dao.MenuDao;
import com.fruit.system.model.Menu;
import com.fruit.system.model.NavMenuVO;
import com.fruit.system.service.MenuService;

@Service("menuService")
public class MenuServiceImpl  extends BaseServiceImpl<Menu> implements MenuService
{
	@Resource(name="menuDao")
	private MenuDao menuDao;
	
	@Resource(name="privilegeService")
	private PrivilegeService privilegeService;
	
	@Override
	public List<Menu> findAllMenus()
	{
		return menuDao.findAllMenus();
	}

	
	public List<NavMenuVO> findMenusForUser(User user)
	{
		List<NavMenuVO> menuVOs=new ArrayList<NavMenuVO>();
		List<Menu> rootMenus=findRootMenus();
		for(Menu rootMenu:rootMenus)
			loopMenu(null, user, menuVOs, rootMenu);
		return menuVOs;
	}
	
	public List<Menu> findRootMenus()
	{
		List<Menu> rootMenus=new ArrayList<Menu>();
		List<Menu> allMenus=findAllMenus();
		for(Menu menu:allMenus)
		{
			if(menu.getParent()==null)
				rootMenus.add(menu);
		}
		Collections.sort(rootMenus);
		return rootMenus;
	}
	
	public List<Menu> findChildrenMenus(Menu parent)
	{
		List<Menu> childrenMenus=new ArrayList<Menu>();
		List<Menu> allMenus=findAllMenus();
		for(Menu menu:allMenus)
		{
			if(menu.getParent()==parent)
				childrenMenus.add(menu);
		}
		Collections.sort(childrenMenus);
		return childrenMenus;
		
	}
	
	//递归查找有权访问的所有菜单，形成树形菜单
	public void loopMenu(NavMenuVO parent,User user,List<NavMenuVO> menuVOs,Menu menu)
	{
		//如果菜单被禁用直接返回
		if(!menu.getMenuEnabled())
			return;
		if(user.hasPrivilege(menu))
		{
			NavMenuVO item=new NavMenuVO();
			item.setId(menu.getId());
			item.setIcon(menu.getIcon());
			item.setInitOpened(menu.getInitOpened());
			item.setName(menu.getName());
			//item.setParent(parent);
			item.setUrl(menu.getUrl());
			menuVOs.add(item);
			List<Menu> childrenMenus=findChildrenMenus(menu);
			if(!CollectionUtils.isEmpty(childrenMenus))
			{
				List<NavMenuVO> children=new ArrayList<NavMenuVO>();
				item.setChildren(children);
				for(Menu child:childrenMenus)
				{
					loopMenu(item, user, children, child);
				}
			}
		}
	}
	
	public void saveMenu(Menu menu)
	{
		Privilege privilege=new Privilege();
		privilege.setCategory("菜单权限");
		privilege.setDescription("创建菜单自动创建对应权限");
		privilege.setName(menu.getName());
		privilege.setUrl(menu.getUrl());
		privilege.setPrivilegeType(PrivilegeTypeEnum.MENU);
		if(menu.getParent()!=null)
		{
			Privilege parent=privilegeService.loadPrivilege(menu.getParent().getPrivilegeId());
			privilege.setParent(parent);
		}
		
		Object[] arr=privilegeService.savePrivilege(privilege);
		if(arr!=null)
		{
			menu.setPrivilegeId(privilege.getId());
			menu.setMenuPos((Integer) arr[0]);
			menu.setMenuCode((Long) arr[1]);
			menuDao.saveEntity(menu);
		}
	}


	@Override
	public BaseDao<Menu> getEntityDao()
	{
		return menuDao;
	}
	
}
